﻿using System;


/// <summary>
/// 资产加载管理器（策略上下文）
/// </summary>
public class AssetLoaderManager
{
    public static AssetLoaderManager Instance;
    private IAssetLoader _loadCurrent;

    public AssetLoaderManager()
    {
        Instance = this;
        this.Init(LoaderType.Resources);
    }

    /// <summary>
    /// 加载策略
    /// </summary>
    private void Init(LoaderType defaultLoaderType)
    {
        _loadCurrent = defaultLoaderType switch
        {
            LoaderType.Resources => new LoadUIFramworkStandard(),
            LoaderType.AssetBundle => new LoadAssetBundle(),
            LoaderType.Addressable => throw new ArgumentOutOfRangeException(nameof(defaultLoaderType)),
            _ => throw new ArgumentOutOfRangeException(nameof(defaultLoaderType))
        };
    }

    /// <summary>
    /// 动态切换|加载策略
    /// </summary>
    /// <param name="type"></param>
    public void Switch(LoaderType type)
    {
        _loadCurrent?.ReleaseAll();
        Init(type);
    }



    public T Load<T>(string key) where T : UnityEngine.Object => _loadCurrent.Load<T>(key);
    public void LoadAsync<T>(string key, Action<T> onCompleted) where T : UnityEngine.Object => _loadCurrent.LoadAsync(key, onCompleted);
    public void Unload(UnityEngine.Object asset) => _loadCurrent.Unload(asset);
    public void ReleaseAll() => _loadCurrent.ReleaseAll();
}

/// <summary>
/// 加载方式枚举
/// </summary>
public enum LoaderType
{
    Resources,
    AssetBundle,
    Addressable
}
